/*
 * created by tim
 *
 */
#include <QApplication>
#include <iostream>
#include <sstream>
#include <fstream>
#include <opencv2/opencv.hpp>
#include <QtCore/QVariant>
#include <QtWidgets/QApplication>
#include <QtWidgets/QLabel>
#include <QtWidgets/QLineEdit>
#include <QtWidgets/QPushButton>
#include <QtWidgets/QWidget>
#include <QTimer>
#include <QWidget>
#include <QImage>
#include <QTextBrowser>

#include "./Time.h"
#include "./ui_time.h"
#include "./time_control.h"

#include "student.h"
#include "./ui_student.h"
#include "./control_student.h"

#include "gemo.h"
#include "./ui_gemo.h"
#include "./binder_gemo.h"

#include "bank.h"
#include "ui_bank.h"


template<typename T>
void quick_sort(T* begin, T* end)//快速排序算法，对 > 已重载的对象使用, 算法复杂度O（log（n））
{
    if (begin > end) return;

    T take= *begin;
    T* b= begin;
    T* e= end;

    do{
        while ((b < e) and (*e > take)) e--;
        if (b < e) {
            *b = *e;
            b++;
        }

        while((b < e) and (*b <= take)) b++;
        if (b < e) {
            *e = *b;
            e--;
        }
    } while (e != b);

    *b = take;

    quick_sort<T>(begin, --b);
    quick_sort<T>(++e, end);
}

void main_Time()
{
    Time myTime = Time(19, 56, 33);
    Time myTime2 = Time(1, 1, 24);

    myTime.display_in_terminal();//展示函数测试
    myTime = myTime + myTime2;
    myTime.display_in_terminal();//加法重载测试
    myTime = myTime - myTime2;
    myTime.display_in_terminal();//减法重载测试
}
void main_Time_show(int args, char* argv[])//含GUI测试
{
    QApplication a(args, argv);
    Time now(0, 0, 0), add(0, 0, 0);
    Ui::ui_time my_ui;
    my_ui.setupUi(&my_ui);
    time_control my_control(&my_ui, &now, &add);

    my_ui.show();
    a.exec();
    delete &now;
    delete &add;
    delete &my_ui;
}
void main_student(std::string pos)
{
    std::ifstream infos;
    infos.open(pos);
    if (infos.is_open()) std::cout<<"yes";
    else std::cout<<"no";
    std::string info_;
    student all[3];
    int cnt = 0;
    double all_avg = 0.0;
    std::string query;

    while (getline(infos, info_)) {
        std::istringstream info(info_);
        all[cnt].set_stu_inf(info);
        all_avg += all[cnt++].Average();
    }
    for (auto x: all) x.print();//显示包括平均分、总分在内所有属性
    all_avg /= cnt;

    quick_sort<student>(all, all + 3);//按总分排序
    std::cout<<"The Best student is:\n";
    all[3].print();//输出最高分
    std::cout<<"All students average's average is: "<<all_avg<<std::endl;
    std::cout<<std::endl<<"Which id you want to know? "<<std::endl;
    std::cin>>query;//根据id查询
    for (auto x: all) x.get_by_id(query);
}
void main_student_show(int args, char* argv[])
{
    QApplication a(args, argv);
    student students[3];
    Ui::student my_ui;
    control_student my_control(&my_ui, students, 3, quick_sort<student>);

    my_ui.show();
    a.exec();
}
void main_gemo()
{
    point my_point(-2, 2);
    line my_line(1, -1, 0);
    show_in_terminal(my_line, my_point);//计算距离测试
}
void main_gemo_show(int args, char* argv[])
{
    QApplication a(args, argv);
    binder1 my_binder;
    my_binder._show();
    a.exec();
}
void main_Bank()
{
    CBank a(10);
    BBank b(20);
    GBank c(30);
    show_in_terminal2(a, b, c);
}
void main_Bank_show(int args, char* argv[])
{
    QApplication a(args, argv);
    Ui::bank my_banks;
    BBank B(10);
    CBank C(20);
    GBank G(30);
    my_banks.setupUi(&my_banks, &B, &C, &G);
    my_banks.show();
    a.exec();
}
int main(int args, char* argv[])
{
    //main_Time_show(args, argv);
    //main_Time();
    //main_student("D:\\Originalcodes\\Field_of_C++\\cpp_class_first_1\\name_list.txt");
    //main_student_show(args, argv);
    //main_gemo();
    //main_gemo_show(args, argv);
    //main_Bank();
    main_Bank_show(args, argv);

    return 0;
}